package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Customer;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.SystemDictionaryItem;
import cn.wolfcode.crm.query.CustomerQuery;
import cn.wolfcode.crm.service.ICustomerService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private ICustomerService customerService;

    @RequestMapping("/potentialList")
    @RequiresPermissions(value = {"customer:potentialList", "潜在客户列表"}, logical = Logical.OR)
    public String potentialList(Model model, @ModelAttribute("qo") CustomerQuery qo) {
        //这里的客户是潜在客户
        qo.setStatus(Customer.CUSTOM_POTENTIAL);
        //如果登录的员工是管理员或者经理就可以看所有的潜在客户
        //但如果是登录的是普通的销售,只能看自己的潜在客户,不能看别人的
        Subject subject = SecurityUtils.getSubject();
        if (!(subject.hasRole("admin") || subject.hasRole("Market_Manager"))) {
            Employee employee = (Employee) subject.getPrincipal();
            qo.setSellerId(employee.getId());
        }
        PageInfo customerList = customerService.list(qo);
        model.addAttribute("result", customerList);
        //查角色是Market或者Market_Manager的员工
        List<Employee> employees = customerService.selectEmployeeByRoleSn("Market", "Market_Manager");
        model.addAttribute("employees", employees);
        //根据目录的sn查客户的所有职业job
        List<SystemDictionaryItem> jobs = customerService.selectJobBysn("job");
        model.addAttribute("jobs", jobs);
        //根据目录的sn查客户的所有来源source
        List<SystemDictionaryItem> sources = customerService.selectJobBysn("source");
        model.addAttribute("sources", sources);
        //根据目录的sn查客户的所有交流方式communicationMethod
        List<SystemDictionaryItem> ccts = customerService.selectJobBysn("communicationMethod");
        model.addAttribute("ccts", ccts);
        return "customer/potentialList";
    }

    @RequestMapping("/poolList")
    public String poolList(Model model, @ModelAttribute("qo") CustomerQuery qo) {
        //这里的客户是潜在客户
        qo.setStatus(Customer.CUSTOM_POOL);
        List<Employee> employees = customerService.selectEmployeeByRoleSn("Market", "Market_Manager");
        model.addAttribute("employees", employees);
        PageInfo customerList = customerService.list(qo);
        model.addAttribute("result", customerList);
        return "customer/poolList";
    }

    @RequestMapping("/failList")
    public String failList(Model model, @ModelAttribute("qo") CustomerQuery qo) {
        //这里的客户是潜在客户
        qo.setStatus(Customer.CUSTOM_FAIL);
        List<Employee> employees = customerService.selectEmployeeByRoleSn("Market", "Market_Manager");
        model.addAttribute("employees", employees);
        PageInfo customerList = customerService.list(qo);
        model.addAttribute("result", customerList);
        return "customer/failList";
    }

    @RequestMapping("/list")
    @RequiresPermissions(value = {"customer:list", "客户列表"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") CustomerQuery qo) {
        PageInfo customerList = customerService.list(qo);
        model.addAttribute("result", customerList);
        //根据目录的sn查客户的所有职业job
        List<SystemDictionaryItem> jobs = customerService.selectJobBysn("job");
        model.addAttribute("jobs", jobs);
        //根据目录的sn查客户的所有来源source
        List<SystemDictionaryItem> sources = customerService.selectJobBysn("source");
        model.addAttribute("sources", sources);
        return "customer/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    @RequiresPermissions(value = {"customer:saveOrUpdate", "客户编辑/保存"}, logical = Logical.OR)
    public JsonResult saveOrUpdate(Customer customer) {
        if (customer.getId() == null) { // 代表新增
            try {
                customerService.save(customer);
            } catch (Exception e) {
                return new JsonResult("电话重复", false);
            }
        } else { // 代表修改
            customerService.update(customer);
        }
        return new JsonResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    @RequiresPermissions(value = {"customer:delete", "客户删除"}, logical = Logical.OR)
    public JsonResult delete(Long id) {
        customerService.delete(id);
        return new JsonResult();
    }
}
